package com.huihua.it.common;
import com.huihua.it.enums.CodeEnum;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.io.Serializable;

/**
 * @author Huihua.Chang
 */
@Data
@NoArgsConstructor
@AllArgsConstructor
public class ResultData<T> implements Serializable {

    private T data;
    private Integer code;
    private String message;

    public ResultData(Integer code, String message) {
        this.code = code;
        this.message = message;
    }

    public static ResultData success(CodeEnum codeEnum) {
        return new ResultData(codeEnum.code, codeEnum.msg);
    }

    public static <T> ResultData<T> success(String msg) {
        return of(null, CodeEnum.SUCCESS.getCode(), msg);
    }

    public static <T> ResultData<T> success(T model, String msg) {
        return of(model, CodeEnum.SUCCESS.getCode(), msg);
    }

    public static <T> ResultData<T> success(T model) {
        return of(model, CodeEnum.SUCCESS.getCode(), "");
    }

    public static <T> ResultData<T> of(T data, Integer code, String msg) {
        return new ResultData<T>(data, code, msg);
    }

    public static <T> ResultData<T> fail(String msg) {
        return of(null, CodeEnum.FAIL.getCode(), msg);
    }

    public static <T> ResultData<T> fail(Integer code, String msg) {
        return of(null, code, msg);
    }

    public static <T> ResultData<T> fail(T model) {
        return of(model, CodeEnum.FAIL.getCode(), "");
    }

    public static <T> ResultData<T> fail(CodeEnum codeEnum) {
        return of(null, codeEnum.code, codeEnum.msg);
    }
}
